/*
 * 文件名：ConsumableUseRecordUseRecordServiceImpl.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-26
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.consumablesInfo.service.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DataAccessException;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.consumablesInfo.dao.ConsumablesInfoDao;
import com.wootide.cms.service.consumables.consumablesInfo.dao.ConsumablesReqRecordDao;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesInfo;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesReqRecord;
import com.wootide.cms.service.consumables.consumablesInfo.service.ConsumablesReqRecordService;
import com.wootide.cms.service.consumables.consumablesInfo.transaction.ConsumablesInfoTransaction;
import com.wootide.cms.service.consumables.consumablesInfo.transaction.ConsumablesReqRecordTransaction;

/**
 * 
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-4-1
 * @see ConsumablesReqRecordServiceImpl
 * @since
 */
public class ConsumablesReqRecordServiceImpl implements
        ConsumablesReqRecordService
{
    /**
     * 运行日志实例
     */
    private RunLog runLog;
    
    /**
     * 易耗品领用记录数据访问层实例
     */
    private ConsumablesReqRecordDao consumablesReqRecordDao;
    
    /**
     * 
     */
    private ConsumablesInfoDao consumablesInfoDao;
    
    /**
     * 
     */
    private ConsumablesInfoTransaction consumablesInfoTransaction;
    
    /**
     * 易耗品领用记录事务层实例
     */
    private ConsumablesReqRecordTransaction consumablesReqRecordTransaction;
    
    public Result addOneConsumablesReqRecord(ConsumablesReqRecord consumablesReqRecord,
            HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("consumablesReqRecord=" + consumablesReqRecord);
        Result result = new Result();
        try
        {
            consumablesReqRecord.setType(0);
            consumablesReqRecordTransaction.addOneConsumablesReqRecord(consumablesReqRecord);
            
            Integer consumablesId = consumablesReqRecord.getConsumablesInfo().getConsumablesId();
            
            ConsumablesInfo tempConsumablesInfo = consumablesInfoDao.findOneConsumablesInfoByConsumableId(consumablesId);
            
            if(tempConsumablesInfo != null)
            {
                if(tempConsumablesInfo.getConsumablesQuantity() == null)
                {
                    tempConsumablesInfo.setConsumablesQuantity(consumablesReqRecord.getQuantity());
                }
                else
                {
                    tempConsumablesInfo.setConsumablesQuantity(tempConsumablesInfo.getConsumablesQuantity()+consumablesReqRecord.getQuantity());
                }
                consumablesInfoTransaction.modOneConsumablesInfo(tempConsumablesInfo);
            }
            
            runLog.info("Add ConsumablesReqRecord Success! Input data:" + consumablesReqRecord, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Add ConsumablesReqRecord Failed! Input data:" + consumablesReqRecord,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add ConsumablesReqRecord Failed! Input Data:" + consumablesReqRecord
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //未知异常
            runLog.error("Add ConsumablesReqRecord Failed!Input data:" + consumablesReqRecord,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result checkContentWhenAdd(String content)
    {
     // 打印方法入口参数
        runLog.debug("content=" + content);
        
        int num = consumablesReqRecordDao.findNumByConsumablesReqRecordName(content);
        Result result = new Result();
        // 如果已存在
        if (num > 0)
        {
            runLog.info("Illegal!ConsumablesReqRecordUseRecord Has Existed! Input Data:" + content);
            
            result.setResultCode(ErrorCodes.Consumables.ConsumablesReqRecord.COMSUMABLESREQRECORD_EXIST);
        }
        // 如果不存在
        else
        {
            runLog.info("Legal!ConsumablesReqRecordUseRecord Not Existed! Input Data:" + content);
            
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        return result;
    }
    
    public Result delConsumablesReqRecords(Integer[] innerIndexs,
            HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            ConsumablesReqRecord consumableUseRecord = null;
            try
            {
                // 调用删除路由事务层
                consumableUseRecord = consumablesReqRecordTransaction.delOneConsumablesReqRecord(innerIndex);
                runLog.info("Delete ConsumablesReqRecord Success! ConsumablesReqRecord = " + consumableUseRecord,
                        request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete ConsumablesReqRecord Failed! Innerindex = " + innerIndex,
                        e,
                        request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (CMSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete ConsumablesReqRecord Failed! Innerindex = " + innerIndex
                        + " Error Message:" + e.getMessage(), request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete ConsumablesReqRecord Failed!  Innerindex = " + innerIndex,
                        e,
                        request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    }
    
    public Result modOneConsumablesReqRecord(ConsumablesReqRecord consumable,
            HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("consumable=" + consumable);
        Result result = new Result();
        try
        {
            ConsumablesReqRecord oldConsumablesReqRecord = consumablesReqRecordTransaction.modOneConsumablesReqRecord(consumable);
            runLog.info("Mod ConsumablesReqRecord Success! Input data:" + consumable
                    + "|oldDate:" + oldConsumablesReqRecord, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod ConsumablesReqRecord Failed! Input data:" + consumable, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod ConsumablesReqRecord Failed! Input Data:" + consumable
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod ConsumablesReqRecord Failed!Input data:" + consumable, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result queryForPages(Page page, QueryCondition queryConditon)
    {
     // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            queryConditon.setObjClass(ConsumablesReqRecord.class);
            QueryResults queryResults = consumablesReqRecordTransaction.queryForPages(page,
                    queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query ConsumablesReqRecord Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query ConsumablesReqRecord Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query ConsumablesReqRecord Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result queryConsumablesReqRecordsById(Integer consumablesInfoId)
    {
        // 打印方法入口参数
        runLog.debug("consumablesInfoId" + consumablesInfoId);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            QueryResults queryResults = consumablesReqRecordDao.findConsumablesReqRecordsById(consumablesInfoId);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query ConsumablesReqRecord Success!");
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query ConsumablesReqRecord Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query ConsumablesReqRecord Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
        
    }
    
    public Result viewOneConsumablesReqRecord(Integer innerIndex)
    {
     // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        
        Result result = new Result();
        try
        {
            ConsumablesReqRecord consumable = consumablesReqRecordTransaction.viewOneConsumablesReqRecord(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(consumable);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One ConsumablesReqRecord Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod ConsumablesReqRecord Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One ConsumablesReqRecord Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    public void setConsumablesReqRecordDao(
            ConsumablesReqRecordDao consumablesReqRecordDao)
    {
        this.consumablesReqRecordDao = consumablesReqRecordDao;
    }

    public void setConsumablesReqRecordTransaction(
            ConsumablesReqRecordTransaction consumablesReqRecordTransaction)
    {
        this.consumablesReqRecordTransaction = consumablesReqRecordTransaction;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public void setConsumablesInfoDao(ConsumablesInfoDao consumablesInfoDao)
    {
        this.consumablesInfoDao = consumablesInfoDao;
    }

    public void setConsumablesInfoTransaction(
            ConsumablesInfoTransaction consumablesInfoTransaction)
    {
        this.consumablesInfoTransaction = consumablesInfoTransaction;
    }
}
